/*
 * Copyright 2008 LIN CHUNGYI
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package xmarsh.foo.zippost.client;

import java.util.List;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;

public class ZipCodeClient {

	private final ZipServiceAsync service = GWT.create(ZipService.class);
	
	final ListBox cityBox = new ListBox();
	final ListBox zoneBox = new ListBox();
	private ChangeHandler handlerZone;
	private int initZoneId = 0;
	private int initCityId = 0;
	private boolean firstLoad = true;
	
	private ChangeHandler handlerCity = new ChangeHandler() {
		@Override
		public void onChange(ChangeEvent event) {
			service.getZipValueList(getCityId(),
					new AsyncCallback<List<ZipValue>>() {
						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(List<ZipValue> result) {
							zoneBox.clear();
							zoneBox.addItem( "--" , "-1");
							for (int i = 0; i < result.size(); i++) {
								ZipValue zv = result.get(i);
								zoneBox.addItem(zv.getName(), zv.getCode());
							}
							
							if(firstLoad){
								// plus 1 for "---"
								zoneBox.setSelectedIndex(initZoneId+1);
								handlerZone.onChange(null);
								firstLoad = false;
							}
						}
					});
		}
	};

	
	public ZipCodeClient(int cityId, int zoneId){
		this.initCityId = cityId;
		this.initZoneId = zoneId;
	}

	public ListBox getCityListBox() {
		cityBox.addChangeHandler(handlerCity);
		return cityBox;
	}

	public ListBox getZoneListBox() {
		return zoneBox;
	}

	public String getCityName() {
		String v = cityBox.getItemText(cityBox.getSelectedIndex());
		return v;
	}

	public String getZoneName() {
		String v = zoneBox.getItemText(zoneBox.getSelectedIndex());
		return v;
	}
	
	public int getZoneId() {
		// add "---"  to first item
		return zoneBox.getSelectedIndex() -1;
	}

	public String getPostCode() {
		String v = zoneBox.getValue(zoneBox.getSelectedIndex());
		return v;
	}

	public int getCityId() {
		//String v = cityBox.getValue(cityBox.getSelectedIndex());
		//return Integer.parseInt(v);
		return cityBox.getSelectedIndex();
	}

	public void setSelectCityId(int cityId){
		for(int i = 0;i<cityBox.getItemCount();i++){
			String v = cityBox.getValue(i);
			if(cityId == Integer.parseInt(v)){
				cityBox.setSelectedIndex(i);
				break;
			}
		}		
	}
	
	public void setSelectZoneId(int zoneId){
		for(int i = 0;i<zoneBox.getItemCount();i++){
			String v = zoneBox.getValue(i);
			if(zoneId == Integer.parseInt(v)){
				zoneBox.setSelectedIndex(i);
				break;
			}
		}		
	}
	
	public void fillLabelById(int cityId, int zoneId, final Label label){
		service.getZipValue(cityId, zoneId, new AsyncCallback<ZipValue>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ZipValue result) {
				label.setText(result.getCityName() + "/" + result.getName() +"/" + result.getCode());
			}
		});
	}

	public void loadData(ChangeHandler zoneHandler) {
		handlerZone = zoneHandler;
		zoneBox.addChangeHandler(handlerZone);		
		
		service.getCityList(new AsyncCallback<List<String>>() {
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(List<String> result) {
				List<String> cityList = result;	
				for (int i = 0; i < cityList.size(); i++) {
					cityBox.addItem(cityList.get(i), "" + i);
				}
				setSelectCityId(initCityId);
				handlerCity.onChange(null);
			}
		});
	}

}
